from pyb import I2C

SLAVE_ADDRESS = 0x42
BAUDRATE = 100000

i2c_slave = I2C(1, I2C.SLAVE, addr=SLAVE_ADDRESS, baudrate=BAUDRATE)

while True:
    try:
        data = i2c_slave.recv(4)
    except OSError as exc:
        if exc.args[0] not in (5, 110):
            # 5 == EIO, occurs when master does a I2C bus scan
            # 110 == ETIMEDOUT
            print(exc)
    except KeyboardInterrupt:
        break
    else:
        print("RECV: %r" % data)